import type { LoginResult } from '@/types/member'
import { defineStore } from 'pinia'
import { ref } from 'vue'

// 定义 Store
export const useMemberStore = defineStore(
  'member',
  () => {
    // 会员信息
    const profile = ref<LoginResult>()

    // 保存会员信息，登录时使用
    const setProfile = (val: LoginResult) => {
      profile.value = val
    }

    // 清理会员信息，退出时使用
    const clearProfile = () => {
      profile.value = undefined
    }

    // 记得 return
    return {
      profile,
      setProfile,
      clearProfile,
    }
  },
  // persist持久化，直接设置为true的话只能保存pc端
  {
    persist: [
      {
        paths: ['profile'], // 只持久化 profile
        storage: {
          getItem: (key: string) => {
            return uni.getStorageSync(key)
          },
          setItem: (key: string, value: string) => uni.setStorageSync(key, value),
        },
      },
    ],
  },
)
